﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using QuestionsAnswers.UI.Models;

namespace QuestionsAnswers.UI.Controllers
{ 
    public class AnswerController : Controller
    {
        private readonly QuestionsAnswersContext _db = new QuestionsAnswersContext();

        //
        // GET: /Answer1/

        public ActionResult Index(int id)
        {
            var answers = _db.Answers.Include(a => a.User).Include(a => a.Question).Where(a=>a.QuestionId ==id);

            ViewBag.Question = _db.Questions.Find(id);

            return View(answers.ToList());
        }

        //[Authorize]
        //public ActionResult MyAnswers()
        //{

        //}


        //
        // GET: /Answer1/Details/5


        //
        // GET: /Answer1/Create
        [Authorize]
        public ActionResult Create(int id)
        {
           
            Question question = _db.Questions.Find(id);
            string user = Membership.GetUser().UserName;
            User userMem = _db.Users.Where(x => x.UserName == user).FirstOrDefault();
           
            Answer answer = new Answer { QuestionId = question.QuestionId, UserId = userMem.UserId ,User=userMem,Question = question};
            return View(answer);
        } 

        //
        // POST: /Answer1/Create
         [Authorize]
        [HttpPost]
        public ActionResult Create(Answer answer)
        {
            if (ModelState.IsValid)
            {
                _db.Answers.Add(answer);
                _db.SaveChanges();
                return RedirectToAction("Index",new {id= answer.QuestionId});  
            }

            return View(answer);
        }
        
        //
        // GET: /Answer1/Edit/5
 
        public ActionResult Edit(int id)
        {
            Answer answer = _db.Answers.Include("User").Include("Question").Where(x=>x.AnswerId ==id).FirstOrDefault();

            return View(answer);
        }

        //
        // POST: /Answer1/Edit/5

        [HttpPost]
        public ActionResult Edit(Answer answer)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(answer).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index",new {id = answer.QuestionId});
            }

            return View(answer);
        }

        //
        // GET: /Answer1/Delete/5
 
        public ActionResult Delete(int id)
        {
            Answer answer =
                _db.Answers.Include(a => a.User).Include(b => b.Question).Where(c => c.AnswerId == id).FirstOrDefault();
            return View(answer);
        }

        //
        // POST: /Answer1/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Answer answer = _db.Answers.Find(id);
            _db.Answers.Remove(answer);
            _db.SaveChanges();
            return RedirectToAction("Index",new {id= answer.QuestionId});
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}